package com.net.oil.product;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.http.AppUrl;
import com.net.oil.product.bean.ProductInfoBean;
import com.net.oil.product.contract.ProductDetailContract;
import com.net.oil.product.presenter.ProductDetailPresenter;
import com.net.oil.util.StringUtil;
import com.net.oil.util.WebViewUtil;
import com.net.oil.util.glide.GlideUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProductDetailActivity extends BaseActivity<ProductDetailPresenter> implements ProductDetailContract.View {

    @BindView(R.id.product_logo)
    ImageView productLogo;
    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;

    private WebSettings webSetting;
    private static String mimeType = "text/html";
    private static String encoding = "UTF-8";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        ButterKnife.bind(this);
        presenter = new ProductDetailPresenter(this);

        initView();
        initData();
    }

    private void initData() {
        int productId = getIntent().getIntExtra(Constant.INTENT_PRODUCT_DETAIL_ID, -1);
        if (productId != -1){
            presenter.getProductDetail(productId);
        }
    }

    private void initView() {
        toolbarTitle.setText("产品详情");

        toolbarGoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        webSetting = webView.getSettings();
        webSetting.setTextZoom(100);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setUseWideViewPort(true);  //将图片调整到适合webview的大小
        webSetting.setLoadWithOverviewMode(true); // 缩放至屏幕的大小


    }

    /**
     * 获取产品详情成功
     *
     * @param productInfoBean 产品详情
     */
    @Override
    public void getProductDetailSuccess(ProductInfoBean productInfoBean) {
        if (productInfoBean != null){
            if (!StringUtil.isEmpty(productInfoBean.getImage())){
                GlideUtil.getInstance().loadImage(this, productLogo, AppUrl.getImageUrl(productInfoBean.getImage()), R.drawable.product_list_err);
            }

            if (!StringUtil.isEmpty(productInfoBean.getContent())){
                webView.loadData(WebViewUtil.getHtmlData(productInfoBean.getContent()), "text/html; charset=UTF-8", null);
            }

        }
    }

    /**
     * 获取产品详情失败
     */
    @Override
    public void getProductDetailFailed() {

    }
}
